﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CocisDBUtility;
using System.Data;
using System.Data.SqlClient;
using CocisModel;

namespace CocisDAL
{
    public class DeptDAL
    {
        #region 判断部门是否存在
        /// <summary>
        /// 判断部门是否存在
        /// </summary>
        /// <param name="names"></param>
        /// <returns></returns>
        public int CheckDeptExsits(string names)
        {
            string sql = "select deptId from department where department='" + names + "'";
            int i = 0;
            SqlDataReader sdr = SQLHelper.ExecuteReader(SQLHelper.ConStr, CommandType.Text, sql, null);
            if (sdr.Read())
            {
                i = 1;

            }
            return i;
        }
        #endregion

        #region 获取所有部门信息 填充到TreeView
        /// <summary>
        /// 获取所有部门信息--填充到TreeView
        /// </summary>
        /// <param name="parentId"></param>
        /// <returns></returns>
        public List<DeptInfo> GetAllDeptTreeView()
        {
            string sql = "select * from departmentInfo ";
            using (SqlDataReader read = SQLHelper.ExecuteReader(SQLHelper.ConStr, System.Data.CommandType.Text, sql, null))
            {
                List<DeptInfo> list = new List<DeptInfo>();
                while (read.Read())
                {
                    DeptInfo s = new DeptInfo();

                    s.DeptId = read.GetInt32(0);
                    s.DeptName = read.GetString(1);
                    list.Add(s);
                }
                return list;
            }

        }
        #endregion

        #region 获取所有部门信息
        /// <summary>
        /// 获取所有部门信息--返回数据集
        /// </summary>
        /// <returns></returns>
        public DataSet GetAllDept()
        {
            string sql = "select * from departmentInfo";
            return SQLHelper.SqlDataAdapter(SQLHelper.ConStr, CommandType.Text, sql, null);

        }

        #endregion

        #region 查询部门信息---通过部门编号
        /// <summary>
        /// 查询部门信息---通过部门编号
        /// </summary>
        /// <param name="deptId"></param>
        /// <returns></returns>
        public DataSet GetDeptInfo(int deptId)
        {
            string sql = "select * from Department where deptId='" + deptId + "'";
            return SQLHelper.SqlDataAdapter(SQLHelper.ConStr, CommandType.Text, sql, null);
        }

        #endregion

        #region 新增部门信息
        /// <summary>
        /// 新增部门信息
        /// </summary>
        /// <param name="parentId"></param>
        /// <param name="deptName"></param>
        /// <returns></returns>
        public int AddDept(string deptName)
        {
            string sql = "insert into departmentInfo(department) values('" + deptName + "')";
            return SQLHelper.ExecuteNonQuery(SQLHelper.ConStr, CommandType.Text, sql, null);
        }

        #endregion

        #region 修改部门信息
        /// <summary>
        /// 修改部门信息
        /// </summary>
        /// <param name="deptId"></param>
        /// <param name="deptName"></param>
        /// <param name="parentId"></param>     
        /// <returns></returns>
        public int UpdateDept(int deptId, string deptName)
        {
            string sql = "update departmentInfo set department='" + deptName + "'  where deptId='" + deptId + "'";
            return SQLHelper.ExecuteNonQuery(SQLHelper.ConStr, CommandType.Text, sql, null);

        }
        #endregion

        #region 删除部门信息
        public int DeleteDept(int deptId)
        {
            string sql = "delete from departmentInfo where deptId='" + deptId + "'";
            return SQLHelper.ExecuteNonQuery(SQLHelper.ConStr, CommandType.Text, sql, null);


        }

        #endregion
    }
}
